﻿using System;
using System.Collections.Generic;
using System.IO;
using wojilu.Apps.Qycms.Domain;
using wojilu.Apps.Qycms.Interface;
using wojilu.Apps.Qycms.Service;
using wojilu.IO;
using wojilu.Web.Context;
using wojilu.Web.Mvc;

namespace wojilu.Web.Controller.Htmls
{
    public class HomeMaker : HtmlMaker
    {
        private static readonly ILog logger = LogManager.GetLogger(typeof(HomeMaker));

        protected override string GetDir(String lng)
        {
            return String.Empty;
        }

        public virtual void Process(String lng)
        {
            String htmlPath = String.Empty;
            String addr = String.Empty;
            String html = String.Empty;
            IConfigsService configsService = new ConfigsService();
            var home_lng = configsService.GetValue("home_lng");
            if (lng == home_lng)
            {
                htmlPath = sys.Path.Root + configsService.GetValue("entrance_file") + "." + configsService.GetValue("file_fileex");
                addr = Link.To(new YBBSiteInitController().Index);
                html = makeHtml(addr);
                FileSystemObject.WriteFile(PathHelper.Map(htmlPath), html);
            }
            else
            {
                switch (lng)
                {
                    case "cn":
                        addr = Link.To(new CnController().Index);
                        break;

                    case "big":
                        addr = Link.To(new Big5Controller().Index);
                        break;

                    case "en":
                        addr = Link.To(new EnController().Index);
                        break;
                }
                htmlPath = sys.Path.Root + configsService.GetValue("file_htmldir") + lng + "/" + configsService.GetValue("entrance_file") + "." + configsService.GetValue("file_fileex");
                html = makeHtml(addr);
                FileSystemObject.WriteFile(PathHelper.Map(htmlPath), html);
            }
            logger.Info("make HomeHtml, url=" + htmlPath+ ", lng=" + lng);
        }

        public virtual void Process()
        {
            String htmlPath = String.Empty;
            String addr = String.Empty;
            String html = String.Empty;
            IConfigsService configsService = new ConfigsService();
            var is_alonelng = configsService.GetValue("is_alonelng").ToInt();
            var home_lng = configsService.GetValue("home_lng");
            if (is_alonelng == 1)
            {
                htmlPath = "/" + configsService.GetValue("entrance_file") + "." + configsService.GetValue("file_fileex");
                addr = Link.To(new YBBSiteInitController().Index);
                html = makeHtml(addr);
                FileSystemObject.WriteFile(PathHelper.Map(htmlPath), html);

                htmlPath = "/" + configsService.GetValue("file_htmldir") + home_lng + "/" + configsService.GetValue("entrance_file") + "." + configsService.GetValue("file_fileex");
                html = makeHtml(addr);
                FileSystemObject.WriteFile(PathHelper.Map(htmlPath), html);
            }
            else
            {
                var lnglist = Lng.find(" isopen =:i order by pid asc,id asc").set("i", 1).list();
                foreach (var row in lnglist)
                {
                    if (row.lng == home_lng)
                    {
                        htmlPath = "/" + configsService.GetValue("entrance_file") + "." + configsService.GetValue("file_fileex");
                        addr = Link.To(new YBBSiteInitController().Index);
                        html = makeHtml(addr);
                        FileSystemObject.WriteFile(PathHelper.Map(htmlPath), html);
                    }
                    switch (row.lng)
                    {
                        case "cn":
                            addr = Link.To(new CnController().Index);
                            break;

                        case "big":
                            addr = Link.To(new Big5Controller().Index);
                            break;

                        case "en":
                            addr = Link.To(new EnController().Index);
                            break;
                    }
                    htmlPath = "/" + configsService.GetValue("file_htmldir") + row.lng + "/" + configsService.GetValue("entrance_file") + "." + configsService.GetValue("file_fileex");
                    html = makeHtml(addr);
                    FileSystemObject.WriteFile(PathHelper.Map(htmlPath), html);
                }
            }
            logger.Info("make AutoHomeHtml, url=" + htmlPath);
        }

    }
}
